package org.kabeja.parser.entities;

import org.kabeja.dxf.entity.DXFEntity;
import org.kabeja.dxf.entity.DXFMLine;
import org.kabeja.dxf.helpers.DXFMLineSegment;
import org.kabeja.dxf.helpers.DXFMLineSegmentElement;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 11:37
 */
public class DXFMLineHandler extends AbstractEntityHandler {
    public static final int GROUPCODE_MLINE_STYLENAME = 2;
    public static final int GROUPCODE_MLINE_STYLE_ID = 340;
    public static final int GROUPCODE_MLINE_SCALE_FACTOR = 40;
    public static final int GROUPCODE_MLINE_JUSTIFICATION = 70;
    public static final int GROUPCODE_MLINE_FLAGS = 71;
    public static final int GROUPCODE_MLINE_NUMBER_OF_VERTICES = 72;
    public static final int GROUPCODE_MLINE_NUMBER_OF_LINESTYLEELEMENTS = 73;
    public static final int GROUPCODE_MLINE_VERTEX_X = 11;
    public static final int GROUPCODE_MLINE_VERTEX_Y = 21;
    public static final int GROUPCODE_MLINE_VERTEX_Z = 31;
    public static final int GROUPCODE_MLINE_DIRECTION_X = 12;
    public static final int GROUPCODE_MLINE_DIRECTION_Y = 22;
    public static final int GROUPCODE_MLINE_DIRECTION_Z = 32;
    public static final int GROUPCODE_MLINE_MITER_DIRECTION_X = 13;
    public static final int GROUPCODE_MLINE_MITER_DIRECTION_Y = 23;
    public static final int GROUPCODE_MLINE_MITER_DIRECTION_Z = 33;
    public static final int GROUPCODE_MLINE_ELEMENT_PARAMETER_COUNT = 74;
    public static final int GROUPCODE_MLINE_ELEMENT_PARAMETER = 41;
    public static final int GROUPCODE_MLINE_FILL_PARAMETER_COUNT = 75;
    public static final int GROUPCODE_MLINE_FILL_PARAMETER = 42;
    protected DXFMLine mLine;
    protected DXFMLineSegment seg;
    protected DXFMLineSegmentElement el;
    protected int index = 0;

    public DXFMLineHandler() {
    }

    @Override
    public String getDXFEntityName() {
        return "MLINE";
    }

    @Override
    public void endDXFEntity() {
    }

    @Override
    public DXFEntity getDXFEntity() {
        return this.mLine;
    }

    @Override
    public boolean isFollowSequence() {
        return false;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case 2:
                this.mLine.setMLineStyleName(var2.getValue());
                break;
            case 10:
                this.mLine.getStartPoint().setX(var2.getDoubleValue());
                break;
            case 11:
                this.seg = new DXFMLineSegment();
                this.mLine.addDXFMLineSegement(this.seg);
                this.seg.getStartPoint().setX(var2.getDoubleValue());
                break;
            case 12:
                this.seg.getDirection().setX(var2.getDoubleValue());
                break;
            case 13:
                this.seg.getMiterDirection().setX(var2.getDoubleValue());
                break;
            case 20:
                this.mLine.getStartPoint().setY(var2.getDoubleValue());
                break;
            case 21:
                this.seg.getStartPoint().setY(var2.getDoubleValue());
                break;
            case 22:
                this.seg.getDirection().setY(var2.getDoubleValue());
                break;
            case 23:
                this.seg.getMiterDirection().setY(var2.getDoubleValue());
                break;
            case 30:
                this.mLine.getStartPoint().setZ(var2.getDoubleValue());
                break;
            case 31:
                this.seg.getStartPoint().setZ(var2.getDoubleValue());
                break;
            case 32:
                this.seg.getDirection().setZ(var2.getDoubleValue());
                break;
            case 33:
                this.seg.getMiterDirection().setZ(var2.getDoubleValue());
                break;
            case 40:
                this.mLine.setScale(var2.getDoubleValue());
                break;
            case 41:
                this.el.setLengthParameter(this.index, var2.getDoubleValue());
                ++this.index;
                break;
            case 42:
                this.el.setFillParameter(this.index, var2.getDoubleValue());
                ++this.index;
                break;
            case 70:
                this.mLine.setJustification(var2.getIntegerValue());
                break;
            case 71:
                this.mLine.setFlags(var2.getIntegerValue());
                break;
            case 73:
                this.mLine.setLineCount(var2.getIntegerValue());
                break;
            case 74:
                this.el = new DXFMLineSegmentElement();
                this.seg.addDXFMLineSegmentElement(this.el);
                this.el.setLengthParameters(new double[var2.getIntegerValue()]);
                this.index = 0;
                break;
            case 75:
                this.el.setFillParameters(new double[var2.getIntegerValue()]);
                this.index = 0;
                break;
            case 340:
                this.mLine.setMLineStyleID(var2.getValue());
                break;
            default:
                super.parseCommonProperty(var1, var2, this.mLine);
        }

    }

    @Override
    public void parseObject(int var1, DXFValue var2, int type) {
        super.parseObjectProperty(var1, var2, type, this.mLine);
    }

    @Override
    public void startDXFEntity() {
        this.mLine = new DXFMLine();
    }
}
